#include <ctime>
//#include <iostream>
#include "customer.h"

using namespace aviation;
using namespace std;

Customer::Customer()
{
	m_title = "AA";
	m_firstName = "AAA";
	m_lastName = "AAA";
	m_address = "AAAAAAAAAA";
	m_passportNumber = "AAAAAAAAA";
	m_teleNumber = "11111111";

	m_ID = util::HashString(AsString());
}

Customer::Customer(String title, String firstName, String lastName, String address, String passportNumber, String teleNumber)
{

	int iCount, iLength;
	char c;
	const int iAsciiOffset = 48;
	
	// Check that the title field is not empty.
	if(title.length() == 0) throw exception("Title field should not be empty.");

	// Check that the first name field is not empty.
	iLength = firstName.length();
	if(iLength<1) throw exception("FirstName field should not be empty.");
	iCount = 0;

	//Check that the last name field is not empty.
	iLength = lastName.length();
	if(iLength<1) throw exception("LastName field should not be empty.");
	iCount = 0;

	//Check that the address field is not empty.
	iLength = address.length();
	if(iLength == 0) throw exception("Address field should not be empty.");
	iCount = 0;

	//Check the length of passport number.
	iLength = passportNumber.length();
	if(iLength != 9) throw exception("Invalid length of passport number.");
	iCount = 0;

	//Checks that the passport number only consists of letters and integers between 0 and 9.
	while(iCount < iLength)
	{
		c = passportNumber[iCount];
		if(!((c >= 'A' && c <= 'Z')
			|| (c >= 'a' && c <= 'z')
			|| (c >= '0' && c <= '9')))
			throw exception("Invalid passport number field.");
		iCount++;
	}

	//Check the length of phone number.
	iLength = teleNumber.length();
	if(!(iLength >= 7 && iLength <= 15)) throw exception("Invalid length of telephone number field.");
	iCount = 0;
	while(iCount < iLength)
	{
		c = teleNumber[iCount];
		if(!(c >= '0' && c <= '9'))
			throw exception("Invalid telephone number field.");
		iCount++;
	}

	//Store details as strings.
	m_title = title;
	m_firstName = firstName;
	m_lastName = lastName;
	m_address = address;
	m_passportNumber = passportNumber;
	m_teleNumber = teleNumber;

	m_ID = util::HashString(AsString());
}

Customer::Customer(const Customer& other)
{
	*this = other;
}

Customer::~Customer()
{
}

//Operators

Customer& Customer::operator=(const Customer& other)
{
	m_title = other.m_title;
	m_firstName = other.m_firstName;
	m_lastName = other.m_lastName;
	m_address = other.m_address;
	m_passportNumber = other.m_passportNumber;
	m_teleNumber = other.m_teleNumber;
	m_ID = other.m_ID;

	return *this;
}

const bool Customer::operator==(const Customer& other) const
{
	return m_ID == other.m_ID;
}

const bool Customer::operator!=(const Customer& other) const
{
	return !(*this == other);
}

const bool Customer::operator<(const Customer& other) const
{
	return m_ID < other.m_ID;
}

//Accessors

const std::string Customer::GetTitle() const
{
	return m_title;
}

const std::string Customer::GetFirstName() const
{
	return m_firstName;
}

const std::string Customer::GetLastName() const
{
	return m_lastName;
}

const std::string Customer::GetAddress() const
{
	return m_address;
}

const std::string Customer::GetPassportNumber() const
{
	return m_passportNumber;
}

const std::string Customer::GetTelephoneNumber() const
{
	return m_teleNumber;
}

const unsigned Customer::GetID() const
{
	return m_ID;
}

const Customer::String Customer::AsString() const
{
	String s = " ";
	return m_title 
		+ s + m_firstName 
		+ s + m_lastName 
		+ s + m_address 
		+ s + m_passportNumber 
		+ s + m_teleNumber;
}
